package com.beiing.tianshuai.tianshuai.mine.ui;

import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.beiing.tianshuai.tianshuai.R;
import com.beiing.tianshuai.tianshuai.base.BaseActivity;
import com.beiing.tianshuai.tianshuai.mine.ui.fragment.PersonalAuditFragment;
import com.beiing.tianshuai.tianshuai.util.DensityUtils;
import com.tianshuai.easypopup.EasyPopup;
import com.tianshuai.easypopup.HorizontalGravity;
import com.tianshuai.easypopup.VerticalGravity;

import java.util.ArrayList;

import butterknife.BindView;

public class MyAuditActivity extends BaseActivity {

    @BindView(R.id.toolbar_title)
    TextView mToolbarTitle;
    @BindView(R.id.toolbar_navigation)
    ImageView mToolbarNavigation;
    @BindView(R.id.toolbar_filter)
    ImageButton mToolbarFilter;
    @BindView(R.id.tab_layout)
    TabLayout mTabLayout;
    @BindView(R.id.view_pager)
    ViewPager mViewPager;

    public static final String TYPE_NAME_ALL = "全部";
    public static final String TYPE_NAME_AUDITING = "审核中";
    public static final String TYPE_NAME_SUCCESS = "已通过";
    public static final String TYPE_NAME_FAILED = "未通过";

    public static final String SEARCH_NAME_ALL = "全部";
    public static final String SEARCH_NAME_ACTIVITY = "活动审核";
    public static final String SEARCH_NAME_DISCOVERY = "发现审核";
    public static final String SEARCH_NAME_VIDEO = "兴趣审核";

    private String[] mTabTitles = {"全部", "审核中", "已通过", "未通过"};

    /**
     * 更多菜单弹窗
     */
    private EasyPopup mPopup;

    private TextView mActionAll;
    private TextView mActionActivity;
    private TextView mActionDiscovery;
    private TextView mActionInterest;
    private ArrayList<Fragment> mFragments;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_my_audit;
    }

    @Override
    protected void init() {

        mPopup = new EasyPopup(mContext)
                .setContentView(R.layout.menu_personal_audit_filter)
                .setAnimationStyle(R.style.QQPopAnim)
                .setFocusAndOutsideEnable(true)
                .createPopup();

        initPopupView();
        initPopupWindow();

        initListener();
        initTabLayout();
    }

    private void initPopupView() {
        mActionAll = mPopup.getView(R.id.action_all);
        mActionActivity = mPopup.getView(R.id.action_activity);
        mActionDiscovery = mPopup.getView(R.id.action_discovery);
        mActionInterest = mPopup.getView(R.id.action_interest);
        mActionAll.setTextColor(ContextCompat.getColor(mContext, R.color.blue_67a0ff));
        mActionAll.setEnabled(false);
    }

    private void initListener() {
        // 返回
        mToolbarNavigation.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        // 筛选
        mToolbarFilter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int i = view.getLayoutParams().width / 2 - DensityUtils.dp2px(mContext, 2);
                mPopup.showAtAnchorView(view, VerticalGravity.BELOW, HorizontalGravity.ALIGN_RIGHT, i, -DensityUtils.dp2px(mContext, 15));
            }
        });
    }

    private void initTabLayout() {
        mFragments = new ArrayList<>();
        mFragments.add(PersonalAuditFragment.newInstance(TYPE_NAME_ALL, SEARCH_NAME_ALL));
        mFragments.add(PersonalAuditFragment.newInstance(TYPE_NAME_AUDITING, SEARCH_NAME_ALL));
        mFragments.add(PersonalAuditFragment.newInstance(TYPE_NAME_SUCCESS, SEARCH_NAME_ALL));
        mFragments.add(PersonalAuditFragment.newInstance(TYPE_NAME_FAILED, SEARCH_NAME_ALL));

        mViewPager.setOffscreenPageLimit(mFragments.size());     //预加载
        mViewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return mFragments.get(position);
            }

            @Override
            public int getCount() {
                return mFragments.size();
            }

            @Override
            public CharSequence getPageTitle(int position) {
                return mTabTitles[position];
            }
        });
        mTabLayout.setupWithViewPager(mViewPager);
    }

    /**
     * 初始化更多菜单
     */
    private void initPopupWindow() {

        mActionAll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mActionAll.setTextColor(ContextCompat.getColor(mContext, R.color.blue_67a0ff));
                mActionAll.setEnabled(false);
                mActionActivity.setTextColor(ContextCompat.getColor(mContext, R.color.textColorDark));
                mActionActivity.setEnabled(true);
                mActionDiscovery.setTextColor(ContextCompat.getColor(mContext, R.color.textColorDark));
                mActionDiscovery.setEnabled(true);
                mActionInterest.setTextColor(ContextCompat.getColor(mContext, R.color.textColorDark));
                mActionInterest.setEnabled(true);
                mToolbarTitle.setText("发布审核");
                if (mFragments != null) {
                    for (Fragment fragment : mFragments) {
                        if (fragment instanceof PersonalAuditFragment) {
                            ((PersonalAuditFragment) fragment).chanFilter(SEARCH_NAME_ALL);
                        }
                    }
                }
                mPopup.dismiss();
            }
        });

        mActionActivity.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mActionActivity.setTextColor(ContextCompat.getColor(mContext, R.color.blue_67a0ff));
                mActionActivity.setEnabled(false);
                mActionAll.setTextColor(ContextCompat.getColor(mContext, R.color.textColorDark));
                mActionAll.setEnabled(true);
                mActionDiscovery.setTextColor(ContextCompat.getColor(mContext, R.color.textColorDark));
                mActionDiscovery.setEnabled(true);
                mActionInterest.setTextColor(ContextCompat.getColor(mContext, R.color.textColorDark));
                mActionInterest.setEnabled(true);
                mToolbarTitle.setText("活动审核");
                if (mFragments != null) {
                    for (Fragment fragment : mFragments) {
                        if (fragment instanceof PersonalAuditFragment) {
                            ((PersonalAuditFragment) fragment).chanFilter(SEARCH_NAME_ACTIVITY);
                        }
                    }
                }
                mPopup.dismiss();
            }
        });

        mActionDiscovery.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mActionDiscovery.setTextColor(ContextCompat.getColor(mContext, R.color.blue_67a0ff));
                mActionDiscovery.setEnabled(false);
                mActionActivity.setTextColor(ContextCompat.getColor(mContext, R.color.textColorDark));
                mActionActivity.setEnabled(true);
                mActionAll.setTextColor(ContextCompat.getColor(mContext, R.color.textColorDark));
                mActionAll.setEnabled(true);
                mActionInterest.setTextColor(ContextCompat.getColor(mContext, R.color.textColorDark));
                mActionInterest.setEnabled(true);
                mToolbarTitle.setText("发现审核");
                if (mFragments != null) {
                    for (Fragment fragment : mFragments) {
                        if (fragment instanceof PersonalAuditFragment) {
                            ((PersonalAuditFragment) fragment).chanFilter(SEARCH_NAME_DISCOVERY);
                        }
                    }
                }
                mPopup.dismiss();
            }
        });

        mActionInterest.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mActionInterest.setTextColor(ContextCompat.getColor(mContext, R.color.blue_67a0ff));
                mActionInterest.setEnabled(false);
                mActionAll.setTextColor(ContextCompat.getColor(mContext, R.color.textColorDark));
                mActionAll.setEnabled(true);
                mActionActivity.setTextColor(ContextCompat.getColor(mContext, R.color.textColorDark));
                mActionActivity.setEnabled(true);
                mActionDiscovery.setTextColor(ContextCompat.getColor(mContext, R.color.textColorDark));
                mActionDiscovery.setEnabled(true);
                mToolbarTitle.setText("兴趣审核");
                if (mFragments != null) {
                    for (Fragment fragment : mFragments) {
                        if (fragment instanceof PersonalAuditFragment) {
                            ((PersonalAuditFragment) fragment).chanFilter(SEARCH_NAME_VIDEO);
                        }
                    }
                }
                mPopup.dismiss();
            }
        });
    }
}